<?php
/**
 * 管理员密码重置工具
 * 访问此页面可以重置管理员密码
 */
// 包含配置和函数文件
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/functions.php';

// 初始化消息变量
$message = '';
$message_type = 'info'; // info, success, error

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];

    // 验证密码
    if (empty($new_password)) {
        $message = '新密码不能为空';
        $message_type = 'error';
    } elseif ($new_password !== $confirm_password) {
        $message = '两次输入的密码不一致';
        $message_type = 'error';
    } elseif (strlen($new_password) < 6) {
        $message = '密码长度不能少于6个字符';
        $message_type = 'error';
    } else {
        // 更新密码
        if (update_admin_password($new_password)) {
            $message = '密码重置成功！新密码是: ' . $new_password;
            $message_type = 'success';
        } else {
            $message = '密码重置失败，请检查文件权限';
            $message_type = 'error';
        }
    }
}

// 生成随机密码作为建议
$suggested_password = generate_random_password();

/**
 * 生成随机密码
 * @param int $length 密码长度
 * @return string 随机密码
 */
function generate_random_password($length = 10) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        $password .= $chars[rand(0, strlen($chars) - 1)];
    }
    return $password;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>重置管理员密码</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        .reset-box {
            max-width: 400px;
            margin: 50px auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        .message {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
        .info {
            background-color: #e3f2fd;
            color: #0d47a1;
        }
        .success {
            background-color: #e8f5e9;
            color: #2e7d32;
        }
        .error {
            background-color: #ffebee;
            color: #b71c1c;
        }
        .suggested-password {
            background-color: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="reset-box">
            <h1>重置管理员密码</h1>
            
            <?php if (!empty($message)): ?>
                <div class="message <?php echo $message_type; ?>"><?php echo $message; ?></div>
            <?php endif; ?>

            <div class="suggested-password">
                <p>建议使用随机密码: <strong><?php echo $suggested_password; ?></strong></p>
            </div>

            <form action="reset_password.php" method="post">
                <input type="password" name="new_password" placeholder="输入新密码" required>
                <input type="password" name="confirm_password" placeholder="确认新密码" required>
                <button type="submit">重置密码</button>
            </form>
            <p class="converter-link"><a href="admin_login.php">返回登录页面</a></p>
        </div>
    </div>
</body>
</html>